import os
import re

KB = 1024
MB = 1024 * KB
GB = 1024 * MB

datasetList = ['TAR', 'WEB', 'RDB', 'VMA', 'VMB']

def matchSingleInt(reStr: str, string: str):
    """
    返回匹配字符串的唯一一个组
    """
    pattern = re.compile(reStr)
    match = pattern.search(string)
    if match is None or len(match.groups()) != 1:
        raise KeyError("match failed")
    return int(match.group(1))

def generateMixedDataPath(dirs: list, dstPath: str) -> None:
    allFiles = []  # list[list[str(filename)]]
    for d in dirs:
        allFiles.append([os.path.join(d, files) for files in os.listdir(d)])
    
    mixedFiles = []
    for i, files in enumerate(allFiles):
        for j, file in enumerate(files):
            mixedFiles.append((file, j/len(files) + 0.00001*i))
    mixedFiles.sort(key=lambda x: x[1])

    with open(dstPath, "w") as f:
        for fileTuple in mixedFiles:
            f.write(fileTuple[0] + "\n")

def printRecord(dir: str):
    print(f"---{dir}---")
    path = f"/data/cbj/result/{dir}.log"
    content = ""
    with open(path, "r") as f:
        content = f.read()
    totalSize = matchSingleInt(r"total size\(B\): (\d+)", content)
    uniqueData = matchSingleInt(r"stored data size\(B\): (\d+)", content)
    print(f"total: {totalSize >> 30}GB, unique: {uniqueData >> 30}GB, dup: {totalSize/uniqueData:.2f}")
    return totalSize, uniqueData


if __name__ == "__main__":
    basicDir = "/data/OdessTest"
    # folders = ["TAR", "WEB", "VMA", "RDB"]#, "VMB"]
    # printAllRecord()
    dss = ["TAR", "WEB", "RDB"]
    total = 0
    unique = 0
    for ds in dss:
        t, u = printRecord(ds)
        total += t
        unique += u
    print(f"---total---")
    print(f"total: {total >> 30}GB, unique: {unique >> 30}GB, dup: {total/unique:.2f}")
    print(f"LRU size: {(unique / 4096 / (4 * MB)):.2f} containers")
    dirList = [os.path.join(basicDir, folder) for folder in dss]
    #generateMixedDataPath(dirList, "data.txt")
